package com.bw.ivideoproject.home.detailFragment

import android.content.Intent
import android.media.Image
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bw.ivideoproject.R
import com.bw.ivideoproject.databinding.FragmentDescriptionBinding
import com.bw.ivideoproject.databinding.ItemRecommendBinding
import com.bw.ivideoproject.entity.SimpleVideoEntity
import com.bw.ivideoproject.home.DetailActivity
import com.bw.ivideoproject.home.HomeViewModel
import com.bw.lib_imageutils.ImageLoaderManager
import com.bw.lib_imageutils.ImageOptions
import com.bw.lib_mvvm.BaseFragment


class DescriptionFragment : BaseFragment<FragmentDescriptionBinding>() {

    private lateinit var video:SimpleVideoEntity
    private lateinit var recommendadapter: RecommendAdapter
    private val viewModel:HomeViewModel by lazy {
        ViewModelProvider(this)[HomeViewModel::class.java]
    }

    override fun bindLayout(): Int {
        return R.layout.fragment_description
    }

    override fun initView() {
        video = arguments?.getSerializable("video") as SimpleVideoEntity
        val option = ImageOptions.Builder().setPlaceImage(R.mipmap.ic_launcher).build()
        ImageLoaderManager.getInstance().loadFileIntoImageView(requireContext(),video.avatar_url,binding.ivHomeDetailDescriptionAuthorimg,option)
        binding.tvHomeDetailDescriptionTitle.text = video.title
        binding.tvHomeDetailDescriptionAuthor.text = video.name

        recommendadapter = RecommendAdapter()
        binding.rvHomeDetailRecommend.apply {
            this.adapter = recommendadapter
            this.layoutManager = LinearLayoutManager(requireContext())
        }
    }

    override fun initData() {
        viewModel.getRecommendVideo(0,15)
    }


    override fun handleObserver() {
        super.handleObserver()
        viewModel.recommendLiveData.observe(this){
            recommendadapter.refresh(it)
        }

        viewModel.filedLiveData.observe(this){
            Toast.makeText(requireContext(),it, Toast.LENGTH_SHORT).show()
        }
    }

    inner class RecommendAdapter():RecyclerView.Adapter<RecommendAdapter.ViewHolder>(){

        var data = mutableListOf<SimpleVideoEntity>()

        inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val subBinding = ItemRecommendBinding.bind(itemView)
        }

        fun refresh(list:List<SimpleVideoEntity>){
            data.addAll(list)
            this.notifyDataSetChanged()
        }

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): RecommendAdapter.ViewHolder {
            val inflate = LayoutInflater.from(parent.context).inflate(R.layout.item_recommend, parent, false)
            return ViewHolder(inflate)
        }

        override fun onBindViewHolder(holder: RecommendAdapter.ViewHolder, position: Int) {
            val entity = data[position]
            val option = ImageOptions.Builder().setPlaceImage(R.mipmap.ic_launcher).build()
            ImageLoaderManager.getInstance().loadFileIntoImageView(requireContext(),entity.videomainimag,holder.subBinding.ivItemRecommend,option)
            holder.subBinding.tvItemRecommend.text = entity.title

            holder.subBinding.root.setOnClickListener{
                val intent = Intent(requireActivity(),DetailActivity::class.java).apply {
                    this.putExtra("video",entity)
                }
                startActivity(intent)
            }
        }

        override fun getItemCount(): Int {
            return data.size
        }
    }

}